<template>
    <com-dialog
        ref="dialog"
        :close-on-click-modal="!needConfirmSign && closeOnClickModal"
        :close-on-press-escape="!needConfirmSign && closeOnPressEscape"
        :show-close="!needConfirmSign && showClose"
        :width="width || '70%'"
        :height="height || '60vh'"
        :title="title"
        scroll-class-name="hiddenX"
        :className="className"
        :top="top"
        v-on="$listeners"
    >
     <div slot="body">
            <div class="dialogPoint" v-if="dialogPointName">{{dialogPointName}}</div>
            <com-form
                ref="form"
                :configs="configs"
                :style="comStyle"
                :inline="inline"
                :label-width="labelWidth"
                :label-suffix="labelSuffix"
                :label-position="labelPosition"
                :init-form-data="initFormData"
                :disabled="disabled"
                :col="col"
                :formId="formId"
                v-on="$listeners"
            />
			<slot name="expand"></slot>
        </div>
        
        <template v-if="!slotRenders.footer" slot="footer">
            <el-button size="mini" @click="cancel()" v-if="showCanceltBut">取消</el-button>
            <el-button
                size="mini"
                type="primary"
                @click="submit()"
                :disabled='lsDisabled'
                v-if="showSubmitBut"
            >
                {{subBtn}}
            </el-button>
            <el-button size="mini" type="primary" @click="commenFun()" v-if="lstest">{{treeBtnText}}</el-button>
        </template>

        <com-functionRender
            v-if="slotRenders.footer"
            slot="footer"
            :render="slotRenders.footer"
        />
    </com-dialog>
</template>

<script>
/* eslint-disable vue/require-prop-type-constructor */
export default {
    props: {
        lsDisabled:{ type: Boolean, default: false },
        treeBtnText:{type: String, default:'连接测试'},
        subBtn:{type: String, default:'确定'},
        showSubmitBut: { type: Boolean, default: true },
        showCanceltBut: { type: Boolean, default: true },
        lstest:{type: Boolean, default: false},
        configs: { type: Array | Function, default: () => [] },
        inline: { type: Boolean, default: false },
        labelWidth: { type: String | Number, default: "" },
        labelPosition: { type: String, default: "right" },
        initFormData: { type: Object },
        col: { type: Number, default: 1 },
        closeOnClickModal: { type: Boolean, default: false },
        closeOnPressEscape: { type: Boolean, default: true },
        showClose: { type: Boolean, default: true },
        needConfirmSign: { type: Boolean, default: false },
        width: { type: String },
        height: { type: String },
        title: { type: String },
        labelSuffix: { type: String, default: "：" },
        slotRenders: { type: Object, default: () => ({}) },
        disabled: Boolean,
        formId: { type: String },
        className: { type: String, default: "newDialogClass" },
        top: { type: String },
        dialogPointName:{type:String,default:''},
        comStyle:{type:String,default:''},
				extraValidate: {type: Function,}
    },
    data() {
        return {};
    },
    methods: {
        show() {
            this.$refs.dialog.show();
        },
        close() {
            this.$emit('close')
            this.$refs.dialog.close();
        },
        cancel() {
            if (!this.needConfirmSign) {
                this.$emit("cancel");
                return;
            }

            this.$confirm("确定取消编辑？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.$emit("cancel");
            });
        },
        async validate() {
            let error = await new Promise((resolve) => {
                this.$refs.form.validate((success, error) => {
                    let flag = true
                    if(this.extraValidate){
                        flag = this.extraValidate()
                    }
                    if (success && flag) {
                        resolve();
                    } else {
                        resolve(error);
                    }
                });
            });

            if (error) {
                throw error;
            }

            return this.$refs.form.formData;
        },
        async submit() {
            await this.validate();
            let formData = this.$refs.form.getHandleFormData();
            this.$emit("submit", formData);
            return formData;
        },
        async commenFun() {
            await this.validate();
            let formData = this.$refs.form.getHandleFormData();
            this.$emit("commenFun", formData);
            return formData;
        },
        async setFormData(data) {
            let form = this.$refs.form;

            if (!form) {
                await new Promise((resolve) => setTimeout(resolve, 100));
                form = this.$refs.form;
            }

            form.setFormData(Object.assign({}, data));
        },
    },
};
</script>
